<?php

/**
 * Controller_goods
 * 物品领用
 * 
 */

class Controller_goods extends MyApp {


	/** 首页 **/
	public function actionIndex()
	{

	}
	//**-------------------------------------- 学生 -----------------------------------**//
	/** 物品领用列表 **/
	public function actiongoodslist ()
	{
		// 分页
//		YOU::loadLib('page', "", '0');
//		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_goods`");
//		$count = $tmp['count'];
//		$page_total = ceil($count/PAGE_LIMIT);
//		// 当前页
//		$p = intval($_GET['p']);
//		if ($p == 0){
//			$p  = 1;
//		}
//		if ($p > 1){
//			$start = PAGE_LIMIT * ($p-1);
//		}else{
//			$start = 0;
//		}

		$goods_lists = $this->db->fetchAll("SELECT * FROM `tbl_goods` ORDER BY goods_id DESC ");
//
//		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
//		$page->next_page   = '下一页>';
//		$page->pre_page    = '<上一页';
//		$page->style_path  = "static/page_style";
//
//		$this->tpl->assign("page", $page->show(4, "yahoo"));
		$this->tpl->assign("goods_lists", $goods_lists);
		$this->tpl->display('goods/m.goods.list.tpl.php');

	}
	/** 物品领用添加 **/
	public function actiongoodsAdd()
	{
		$goods_lists = $this->db->fetchAll("SELECT * FROM `tbl_goods`");

		if ($_POST){
            $goods_name   	    = $this->post->goods_name;
            $goods_date  	    = $this->post->goods_date;
            $goods_content  	= $this->post->goods_content;              
            $goods_use          = $this->post->goods_use;
//             if (empty($goods_name) || empty($goods_content)) {
//
//            	Helper_Client::alert("所有项目必须填满！");
//                   }
            if(empty($goods_name)){
                $goods_name =  $this->model->user->getNameById($this->user['user_id']);
            }
			// 写入数据库
			$info = array(

			"goods_name"            =>$goods_name,
			"goods_date"            =>$goods_date,
			"goods_content"         =>$goods_content,			
			"goods_use"             =>$goods_use,
			
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);
			$this->db->do_insert("tbl_goods", $info);
			
			// 跳转
			Helper_Client::location("添加物品领用成功", "index.php?a=goods.goodslist&goods_id={$goods_id}");
		}

		$this->tpl->assign("goods_lists", $goods_lists);
		$this->tpl->display('goods/m.goods.add.tpl.php');
	}
	/** 修改物品领用 **/
	public function actiongoodsUpdate()
	{

		$goods_id   = (int)$this->get->goods_id;
		$goods_data = $this->db->fetchROW("SELECT * FROM `tbl_goods` WHERE `goods_id`='{$goods_id}'");

		if (empty($goods_data)){
			Helper_Client::alert("领用物品不存在！");
		}

		if ($_POST){

            $goods_name   	    = $this->post->goods_name;
            $goods_date  	    = $this->post->goods_date;
            $goods_content  	= $this->post->goods_content;              
            $goods_use          = $this->post->goods_use;
           if (empty($goods_name) || empty($goods_content)) {
  
            	Helper_Client::alert("所有项目必须填满！");
                 }
          
   
			// 写入数据库
			$info = array(
			"goods_name"            =>$goods_name,
			"goods_date"            =>$goods_date,
			"goods_content"         =>$goods_content,			
			"goods_use"             =>$goods_use,
			
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",
			);
			$this->db->do_update("tbl_goods", $info, "goods_id='{$goods_id}'");

			// 跳转
			Helper_Client::location("修改物品领用成功", "index.php?a=goods.goods	list&goods_id={$goods_id}");
		}

		$this->tpl->assign("goods_data", $goods_data);
		$this->tpl->display('goods/m.goods.update.tpl.php');
	}
	/** 删除维修部门资料 **/
	public function actiongoodsDel()
	{
		$goods_id = $this->get->goods_id;
		$this->db->query("DELETE  FROM `tbl_goods` WHERE goods_id='{$goods_id}'");

		// 跳转
		Helper_Client::location("删除物品领用成功", "index.php?a=goods.goodslist");
	}
	

}

?>